package com.zllkj.mm.module.shopping.fragment

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import com.zllkj.mm.R
import com.zllkj.mm.api.network.RetrofitHelper
import com.zllkj.mm.api.network.RxHelper
import com.zllkj.mm.api.network.subscriber.ProgressSubscriber
import com.zllkj.mm.base.BaseTitleFragment
import com.zllkj.mm.databinding.FragmentAfterSaleDetailBinding
import com.zllkj.mm.model.entity.AfterSaleDetailVo
import com.zllkj.mm.module.shopping.vm.AfterSaleOrderDetailVM
import com.zllkj.mm.util.common.FormatUtil
import kotlinx.android.synthetic.main.layout_topbar.view.*
import java.util.*

class AfterSaleGoodsDetailFragment() : BaseTitleFragment() {

    var vm = AfterSaleOrderDetailVM()


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        var binding = DataBindingUtil.inflate<FragmentAfterSaleDetailBinding>(layoutInflater,R.layout.fragment_after_sale_detail,null,false)
        binding.afterSaleDetailVM = vm;
        mRootView = binding.root
        mRootView.tv_title_right.visibility = View.INVISIBLE
        mRootView.tv_title_center.text = "售后详情"
        mRootView.fl_title_left.setOnClickListener { removeFragment() }
        loadData()
        return mRootView

    }

    override fun loadData() {
        super.loadData()


        addSubscribe(RetrofitHelper.getInstance().orderApi.getAfterSaleOrderDetail((HashMap<String, Any>().apply {
            this["afterSaleId"] = arguments?.getString("orderId").toString()
        })).compose<AfterSaleDetailVo>(RxHelper.defaultHandleResult<AfterSaleDetailVo>()).subscribe(object : ProgressSubscriber<AfterSaleDetailVo>(requireContext()){
            override fun onSucc(t: AfterSaleDetailVo?) {
                vm.shopName.set(t?.shopName)
                vm.orderStatus.set(FormatUtil.getAfterSaleOrderStatus(t?.afterSaleStatus))
                vm.afterSaleNo.set(t?.afterSaleNo.toString())
                vm.afterSaleId.set(t?.afterSaleId.toString())
                vm.originalOrderNo.set(t?.orderNo.toString())
                vm.originalOrderId.set(t?.orderId.toString())

                vm.applyTime.set(t?.createTime.toString())
                vm.refundReason.set(t?.refundReason.toString())
                vm.refundPrice.set(FormatUtil.bigDecimalUtilFormat(t?.totalRefundFee.toString(),"100"))

                t?.afterSaleGoodsList?.let {
                    vm.goodsName.set(it[0].goodsName)
                    vm.picUrl.set(it[0].goodsPic)

                    vm.buyCount.set(it[0].buyCount.toString() + "件数")
                    vm.unitPrice.set(FormatUtil.bigDecimalUtilFormat(it[0].unitPrice.toString(),"100"))
                }

            }

            override fun onError(e: Throwable?) {
                super.onError(e)
                Log.d("acthome","error")
            }

        }))
    }

    override fun getContentLayoutId(): Int {
        return R.layout.fragment_after_sale_detail
    }
}